<template>
  <div style="padding: 25px;">
    <!--    面包屑-->
    <el-breadcrumb separator="/" style="margin-bottom: 12px">
      <el-breadcrumb-item :to="{ path: '/' }">我的主页</el-breadcrumb-item>
      <el-breadcrumb-item>历史数据</el-breadcrumb-item>
    </el-breadcrumb>

    <!--    功能区域-->
    <div style="margin: 10px 0">
      <el-popconfirm
          title="确定删除吗？"
          @confirm="deleteBatch">
        <template #reference>
          <el-button type="danger" plain>批量删除</el-button>
        </template>
      </el-popconfirm>
    </div>

    <!--    数据展示区域-->
    <el-card>
      <el-table
          :data="tableData"
          border
          @selection-change="handleSelectionChange"
          style="width: 99%">
        <el-table-column
            type="selection"
            width="60">
        </el-table-column>
        <el-table-column
            prop="id"
            label="ID"
            width="100">
        </el-table-column>
        <el-table-column
            prop="temp"
            label="温度">
        </el-table-column>
        <el-table-column
            prop="humi"
            label="湿度">
        </el-table-column>
        <el-table-column
            prop="smog"
            label="烟雾">
        </el-table-column>
        <el-table-column
            prop="light"
            label="光强">
        </el-table-column>
        <el-table-column
            prop="sound"
            label="噪声">
        </el-table-column>
        <el-table-column
            prop="time"
            label="记录时间">
        </el-table-column>
        <el-table-column label="功能">
          <template #default="scope">
            <el-popconfirm title="确定删除吗？" @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button size="small" plain type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--    分页信息-->
    <div style="margin-top: 20px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "History",
  data() {
    return {
      tableData: [],
      abnormalDataIds: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
    }
  },
  mounted() {
    this.load()
  },
  methods: {
    load() {
      request.get("/officeData/findPage", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    handleDelete(id) {
      request.delete("/officeData/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load()  // 删除之后重新加载表格的数据
      })
    },
    deleteBatch() {
      if (!this.ids.length) {
        this.$message.warning("请选择数据！")
        return
      }
      request.post("/officeData/deleteBatch", this.ids).then(res => {
        if (res.code === '0') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleSelectionChange(val) {
      this.ids = val.map(v => v.id)   // [{id,name}, {id,name}] => [id,id]
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      this.load()
    },
  }
}
</script>

<style scoped>

</style>